<?php

namespace App\Domain\Permissions\Controllers;

use App\Http\Controllers\Controller;
use App\Supports\Formatter\ArrayFormatter;
use Illuminate\Http\Request;
use App\Domain\Permissions\Services\PermissionsService;

class PermissionsController extends Controller
{

    /**
     * 获取用户菜单权限
     */
    public function getAccountMenu(Request $request)
    {
        $this->validate($request, [
            'type' => 'required|in:1,2',
            'roleId' => 'required',
        ]);
        $params = [
            'type' => $request->input('type'),
            'roleId' => $request->input('roleId'),
        ];
//        dd($params);
        $data = (new PermissionsService())->getPermissionMenu($params);
        $keyMap = [
            'id' => 'id',
            'name' => 'name',
            'path' => 'sub_path',
            'type' => 'type',
            'hasAuthority' => 'hasAuthority',
            'title' => 'label',
            'icon' => 'icon',
            'activeIcon' => 'active_icon',
            'authorityList' => 'authorityList',
        ];
        return $this->success([
            'list' => ArrayFormatter::treeArrayConvertKey($data['list'], $keyMap, 'authorityList'),
            'hasAllAuthority' => $data['hasAllAuthority']
        ]);
    }

    /**
     * 获取所有权限（菜单和按钮）
     * @return
     */
    public function getPermissionTree()
    {
        $appName = "pc";
        $data = (new PermissionsService())->getPermissionTree($appName);
        return $this->success(['list' => $data], '获取菜单成功');
    }
}
